//
//  YbrChannelDao.m
//  wifibox
//
//  Created by os X 10.9 on 14-11-15.
//  Copyright (c) 2014年 yinbaner. All rights reserved.
//  频道管理

#import "YbrChannelDao.h"
#import "YbrChannelItem.h"

// 这里所有的ChannelArr都是我们   isSele是否是选择的  type表示是哪个类型的(音乐墙还是课程)
@implementation YbrChannelDao

// 获得现在应该处理的时哪个数据库
-(NSString *) whichDb:(int) type {
    if(type == CourseDB) {
        return @"course_channel";
    }else if(type == MusicDB){
        return @"music_channel";
    }
    return @"";
}

// 清空数据库
-(BOOL) clearDb:(int) type {
    __block BOOL isOK = YES;

    [[YbrSQLHelper getDataBase] inTransaction:^(FMDatabase *db, BOOL *rollback) {
        [db executeUpdate:[NSString stringWithFormat:@"DELETE FROM %@;"  ,[self whichDb:type]]];
        BOOL isRunOK = [db executeUpdate:[NSString stringWithFormat:@"UPDATE sqlite_sequence set seq=0 where name='%@';" ,[self whichDb:type]]];
        if(!isRunOK) {
            isOK = NO;
            *rollback = YES;
        }

    }];
    return isOK;
}

// 添加到数据库
-(BOOL) addCache:(NSArray *)channelArr type:(int)type userSort:(BOOL)userSort{
    __block BOOL isOK = YES;
    
    if(channelArr != nil) {
        [[YbrSQLHelper getDataBase] inTransaction:^(FMDatabase *db, BOOL *rollback) {
            
            for (int i = 0; i < [channelArr count]; i++) {
                YbrChannelItem *channel = channelArr[i];
                BOOL isRunOK;
                if(type == CourseDB) {
                    isRunOK = [db executeUpdateWithFormat:@"insert into course_channel (name ,id , course_type,orderId ,selected) VALUES (%@ ,%i ,%i ,%i ,%i);",channel.name ,channel.ID , channel.course_type ,userSort ? channel.orderId : i, channel.selected];
                }else if (type == MusicDB) {
                    isRunOK = [db executeUpdateWithFormat:@"insert into music_channel (name ,id ,course_type,orderId ,selected) VALUES (%@ ,%i ,%i ,%i ,%i);",channel.name ,channel.ID ,channel.course_type ,userSort ? channel.orderId : i ,channel.selected];
                }
                
                if(!isRunOK) {
                    isOK = NO;
                    *rollback = YES;
                    break;
                }
            }
            
        
        }];
    }
    return isOK;
}

// 列出列表
-(NSMutableArray *) listCache:(BOOL)isSele type:(int) type {
    __block NSMutableArray *arr;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        if(type == CourseDB) {
            rs = [db executeQueryWithFormat:@"select * from course_channel where selected=%i",isSele];
        }else if (type == MusicDB) {
            rs = [db executeQueryWithFormat:@"select * from music_channel where selected=%i" ,isSele];
        }
        
        NSMutableArray *tempArr = [NSMutableArray array];
        while (rs.next) {
            YbrChannelItem *channel = [[YbrChannelItem alloc] init];
            channel.ID = [rs intForColumn:@"id"];
            channel.name = [rs stringForColumn:@"name"];
            channel.course_type = [rs intForColumn:@"course_type"];
            channel.selected = [rs boolForColumn:@"selected"];
            channel.orderId = [rs intForColumn:@"orderId"];
            [tempArr addObject:channel];
        }
        arr = tempArr;
    }];
    return arr;
}

@end